pub(crate) mod command;
pub(crate) mod proxy;
pub(crate) mod wasm;

mod manager;
mod monitor;

pub use manager::*;
pub use monitor::*;

use model_graph_types::container::process::Process;
use std::borrow::BorrowMut;
use std::collections::HashMap;
use std::process::{id, Child, Command, Output, Stdio};
use std::sync::{Arc, Mutex, RwLock};
use tokio::task::JoinHandle;

lazy_static::lazy_static! {
    pub(crate) static ref MAX_PID: RwLock<u32> = RwLock::new(0);

    pub(crate) static ref RUNNING_PROCESSES: RwLock<HashMap<String, (Process,JoinHandle<u32>,Option<Arc<Mutex<Child>>>)>> =
        RwLock::new(HashMap::new());
}

pub(crate) fn generate_pid() -> anyhow::Result<u32> {
    let mut max_pid = MAX_PID.write().map_err(|err| anyhow::anyhow!("{}", err))?;

    *max_pid = *max_pid + 1;
    Ok(*max_pid)
}
